package com.zhukunri.sp04.service;

import com.zhukunri.sp01.domain.Item;
import com.zhukunri.sp01.domain.Order;
import com.zhukunri.sp01.domain.User;
import com.zhukunri.sp01.service.OrderService;
import com.zhukunri.sp01.web.util.JsonResult;
import com.zhukunri.sp04.fegin.ItemClient;
import com.zhukunri.sp04.fegin.UserClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private UserClient userClient;
    @Autowired
    private ItemClient itemClient;



    //根据订单的id，获取订单
    @Override
    public Order getOrder(String id) {


        log.info("获取订单，id="+id);

        Order order = new Order();
        order.setId(id);
        //从微服务item-service中，获取item数据
        JsonResult<List<Item>> items = itemClient.getItems(id);

        //从微服务user-service中，获取user数据
        JsonResult<User> user = userClient.getUser(8);//真实项目中获取已经登录的用户id

        order.setItems(items.getData());
        order.setUser(user.getData());

        return order;
    }



    //添加订单
    @Override
    public void addOrder(Order order) {
        log.info("添加订单，order="+order);

        //调用远程商品，减少商品
        itemClient.descreaseNumber(order.getItems());

        //调用远程用户，增加积分
        userClient.addScore(order.getUser().getId(),1000 );
    }
}
